const loginDao = require("../dao/loginDao");
const { code } = require("../config/key");
const loginUtil = require('../util/loginUtil')

module.exports = {
    findOne: async(body, url) => {
        const { username, password } = body;
        const user = await loginDao.findOne(username);
        //console.log(user)
        //console.log(code(password))
        if (!user)
            return loginUtil.USER_NOTFOUND
        else if (code(password) !== user.password)
            return loginUtil.PASSWORD_ERR
        else if (user.status === 3)
            return loginUtil.STATUS_ERR
        else if (url === '/admin' && user.role === "2")
            return loginUtil.ROLE_ERR
        return loginUtil.LOGIN_SUCCESS(user)
    },
};